package com.summer.ioc.util;

import com.summer.ioc.reflect.Parameter;
import org.springframework.core.DefaultParameterNameDiscoverer;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * @author xiaoye
 * @create 2021-05-13 16:32
 */
public class MethodUtil {

    private static DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();

    public static Parameter[] getParameters(Method method)
    {
        String[] parameterNames = discoverer.getParameterNames(method);
        var parameters = method.getParameters();
        Parameter[] params = new Parameter[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            params[i] = new Parameter(parameterNames[i],parameters[i]);
        }
        return params;
    }

    public static Parameter[] getParameters(Constructor constructor)
    {
        String[] parameterNames = discoverer.getParameterNames(constructor);
        var parameters = constructor.getParameters();
        Parameter[] params = new Parameter[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            params[i] = new Parameter(parameterNames[i],parameters[i]);
        }
        return params;
    }
}
